package logic

import (
	"web_app/dao/mysql"
	"web_app/modules"
	"web_app/pkg/snowflake"
)

func SignUp(p *modules.ParamSignUp) error {
	//先判断用户名是否存在
	if err := mysql.QueryUserByUsername(p.Username); err != nil {
		return err
	}
	//生成uid
	userID := snowflake.GenID()

	user := &modules.User{UserID: userID,
		Username: p.Username,
		Password: p.Password,
	}
	//再进行插入数据
	if err := mysql.InsertUser(user); err != nil {
		return err
	}
	return nil
}

func Login(p *modules.ParamLogin) (error, *modules.User) {
	user := &modules.User{
		Username: p.Username,
		Password: p.Password,
	}
	var resultuser *modules.User
	var err error
	//发生错误就返回错误
	if err, resultuser = mysql.Login(user); err != nil {
		return err, nil
	}

	//没发生错误就返回nil
	return nil, resultuser
}
